package com.wsoft.manager;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wsoft.constant.redis.RedisKeyConstant;
import com.wsoft.core.annotation.GetRedisCache;
import com.wsoft.core.service.RedisService;
import com.wsoft.entity.TsModuleEntity;
import com.wsoft.mapper.TsModuleMapper;
import com.wsoft.query.module.ModuleListQuery;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author z001
 * @Date 2024/10/23 09:08
 */
@Service
public class TsModuleManager extends ServiceImpl<TsModuleMapper, TsModuleEntity> {

    @Resource
    private RedisService redisService;

    @GetRedisCache(type = RedisKeyConstant.Module.MODULE_CODE,
            expire_sec = RedisKeyConstant.Module.EXPIRED,
            value = RedisKeyConstant.REDIS_SELF)
    public TsModuleEntity getByCode(String code){
        if(StrUtil.isBlank(code)){
            return null;
        }
        QueryWrapper<TsModuleEntity> query = new QueryWrapper<>();
        query.lambda().eq(TsModuleEntity::getCode,code);
        return getOne(query);
    }

    /**
     * 根据id修改
     * @param entity
     * @return
     */
    @Override
    public boolean updateById(TsModuleEntity entity){
        TsModuleEntity oldEntity = this.getById(entity.getId());
        boolean b = super.updateById(entity);
        if(b){
            redisService.del(RedisKeyConstant.Module.MODULE_CODE + oldEntity.getCode());
            redisService.del(RedisKeyConstant.Module.MODULE_ID + entity.getId());
        }
        return b;
    }


    /**
     * 根据id获取
     * @param id
     * @return
     */
    @GetRedisCache(type = RedisKeyConstant.Module.MODULE_ID,
            expire_sec = RedisKeyConstant.Module.EXPIRED,
            value = RedisKeyConstant.REDIS_SELF)
    public TsModuleEntity getById(Long id){
        return super.getById(id);
    }

    /**
     * 根据id删除
     * @param id
     * @return
     */
    public boolean removeById(Long id){
        TsModuleEntity entity = getById(id);
        boolean b = super.removeById(id);

        if(b){
            redisService.del(RedisKeyConstant.Module.MODULE_CODE + entity.getCode());
            redisService.del(RedisKeyConstant.Module.MODULE_ID + id);
        }
        return b;
    }

    /**
     * 列表查询
     * @param query
     * @return
     */
    public List<TsModuleEntity> listModule(ModuleListQuery param) {
        QueryWrapper<TsModuleEntity> query = new QueryWrapper<>();
        query.lambda().like(StrUtil.isNotBlank(param.getCode()),TsModuleEntity::getCode,param.getCode())
                .like(StrUtil.isNotBlank(param.getName()),TsModuleEntity::getName,param.getName())
                .eq(null != param.getIsThird(),TsModuleEntity::getIsThird,param.getIsThird())
                .eq(null != param.getIsToken(),TsModuleEntity::getIsToken,param.getIsToken())
                .eq(null != param.getIsSign(),TsModuleEntity::getIsSign,param.getIsSign())
                .eq(null != param.getIsEnable(),TsModuleEntity::getIsEnable,param.getIsEnable())
                .orderByAsc(TsModuleEntity::getSort);
        return list(query);
    }
}